#include "eboard.h"

EBoard::EBoard(int x,int y,int w,int h) : Fl_Gel_Tabs(x,y,w,h)
{
    box(FL_PLASTIC_DOWN_BOX);
    color(FL_SELECTION_COLOR);
    int gx=x+5, gy=y+20, gw=w-10, gh=h-20;
    design=new Fl_Group(gx,gy,gw,gh,"Dizayn Penceresi");
    design->color(FL_SELECTION_COLOR);
    design->end();
    Fl::add_timeout(0.10,deleteTimer_CB,this);

}

EBoard::~EBoard()
{

}

int EBoard::handle(int e)
{
    Ewindow *sahip=dynamic_cast<Ewindow *>(parent());
    int ret=Fl_Gel_Tabs::handle(e);
    switch(e)
    {
        case FL_PUSH:
            if(Fl::event_button2())
            {
                eContainer *comp=new eContainer(Fl::event_x(),Fl::event_y(),150,45,sahip->toolGonder()->tipGonder());
                comp->end();
                elemanEkle(comp);
                design->add(comp);
                design->redraw();
                sahip->redraw();
            }
            return 1;
    }

    return ret;

}
void EBoard::elemanEkle(eContainer *p)
{
    if(p->tipgonder()=="Radio")
    {
        p->icerikgonder()->callback(radioClick_CB,this);
        radioTut.push_back(p);
    }
    else if(p->tipgonder()=="Check")
        checkTut.push_back(p);
    else if(p->tipgonder()=="Label")
        labelTut.push_back(p);
    else if (p->tipgonder()=="Metin")
        metinTut.push_back(p);
    else if(p->tipgonder()=="Resim")
        resimTut.push_back(p);
    else if(p->tipgonder()=="Anim")
        animTut.push_back(p);
}

void EBoard::deleteTimer_CB(void *userdata)
{
    EBoard *p=(EBoard *)userdata;
    p->deleteTimer_CB2();
}
void EBoard::deleteTimer_CB2()
{
    eContainer *gecici;
    std::vector<eContainer *>::iterator gecicit;
    for(int i=0;i<radioTut.size();i++)
    {
        gecici=radioTut[i];
        if(gecici->deletesil())
        {
            design->remove(gecici);
            gecicit=radioTut.begin()+i;
            radioTut.erase(gecicit);
            delete gecici;
        }
    }

    for(int i=0;i<checkTut.size();i++)
    {
        gecici=checkTut[i];
        if(gecici->deletesil())
        {
            design->remove(gecici);
            gecicit=checkTut.begin()+i;
            checkTut.erase(gecicit);
            delete gecici;
        }
    }
    for(int i=0;i<labelTut.size();i++)
    {
        gecici=labelTut[i];
        if(gecici->deletesil())
        {
            design->remove(gecici);
            gecicit=labelTut.begin()+i;
            labelTut.erase(gecicit);
            delete gecici;
        }
    }
    for(int i=0;i<metinTut.size();i++)
    {
        gecici=metinTut[i];
        if(gecici->deletesil())
        {
            design->remove(gecici);
            gecicit=metinTut.begin()+i;
            metinTut.erase(gecicit);
            delete gecici;
        }
    }
    for(int i=0;i<resimTut.size();i++)
    {
        gecici=resimTut[i];
        if(gecici->deletesil())
        {
            design->remove(gecici);
            gecicit=resimTut.begin()+i;
            resimTut.erase(gecicit);
            delete gecici;
        }
    }
    for(int i=0;i<animTut.size();i++)
    {
        gecici=animTut[i];
        if(gecici->deletesil())
        {
            design->remove(gecici);
            gecicit=animTut.begin()+i;
            animTut.erase(gecicit);
            delete gecici;
        }
    }
    redraw();
    fl_cursor(FL_CURSOR_DEFAULT);
    Fl::repeat_timeout(0.10,deleteTimer_CB,this);
}

void EBoard::radioClick_CB(Fl_Widget *p,void *userdata)
{
    EBoard *gecici=(EBoard *)userdata;
    gecici->radioClick_CB2(p);
}
void EBoard::radioClick_CB2(Fl_Widget *p)
{
    Fl_Round_Button *geciciradio;
    for(int i=0;i<radioTut.size();i++)
    {
        geciciradio=(Fl_Round_Button *)radioTut[i]->icerikgonder();
        geciciradio->value(0);
    }
    geciciradio=(Fl_Round_Button *)p;
    geciciradio->value(1);

}
